package com.sino.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sino.bean.Basicinfo;
import com.sino.bean.Qxkw;
import com.sino.bean.Tjyj;
import com.sino.bean.Users;
import com.sino.dao.IPatentManageDAO;

public class PatentManageDAO extends HibernateDaoSupport implements
		IPatentManageDAO {

	// 推荐单位管理
	public List findQxkwByName2(String hql) {

		return this.getHibernateTemplate().find(hql);
	}

	public List findQxkwByName(String qxkwmc) {
		String strSQL = "from  Qxkw as q where q.name=?";

		return this.getHibernateTemplate().find(strSQL, qxkwmc);
	}

	public List findQxkwByName1(String qxkwmc) {
		String strSQL = "from  Qxkw as q where q.name like ?";

		return this.getHibernateTemplate().find(strSQL, qxkwmc);
	}

	public void deleteQxkwType(String id) {
		Qxkw qxkw = (Qxkw) this.getHibernateTemplate().get(Qxkw.class, id);
		this.getHibernateTemplate().delete(qxkw);
	}

	public List findQxkwType(String type) {
		String hpl = "from Qxkw as qxkw where length(qxkw.id)<3";
		List qxkwList = null;
		try {
			qxkwList = this.getHibernateTemplate().find(hpl);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return qxkwList;
	}

	public void saveQxkwType(Qxkw qxkw) {
		try {
			this.getHibernateTemplate().save(qxkw);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void deleteQxkw(String id) {
		try {
			Qxkw qxkw = (Qxkw) this.getHibernateTemplate().get(Qxkw.class, id);
			this.getHibernateTemplate().delete(qxkw);

		} catch (RuntimeException re) {
			log.error("deleteQxkw--error", re);
			throw re;
		}

	}

	public List<Qxkw> findQxkw() {
		List<Qxkw> qxkwList = null;
		String hql = "from Qxkw as qxkw where length(qxkw.id)>=3";
		try {
			qxkwList = this.getHibernateTemplate().find(hql);

		} catch (HibernateException e) {
			log.error("findQxkw---error");
			throw e;
		}
		return qxkwList;
	}

	public void saveQxkw(Qxkw qxkw) {
		try {
			this.getHibernateTemplate().save(qxkw);

		} catch (RuntimeException re) {
			log.error("saveQxkw", re);
			throw re;
		}

	}

	public Qxkw findQxkwById(String id) {
		String strSQL = "from  Qxkw q where q.id=?";
		Qxkw qxkw = null;
		try {
			qxkw = (Qxkw) this.getHibernateTemplate().get(Qxkw.class, id);

		} catch (RuntimeException re) {
			log.error("findQxkwById", re);
			throw re;
		}
		return qxkw;

	}

	public void updateQxkw(Qxkw qxkw) {

		this.getHibernateTemplate().update(qxkw);

	}

	// 用户管理
	public List<Users> findUser(String qxkw, String hql) {
		List<Users> userList = null;

		try {
			userList = this.getHibernateTemplate().find(hql);

		} catch (HibernateException e) {
			log.error("findUser---error");
			throw e;
		}
		return userList;
	}

	public Tjyj findTjyjByZlid(String Zlid) {
		String strSQL = "from  Tjyj t where t.zlid=?";
		Tjyj tjyj = null;
		try {
			List<Tjyj> tList = this.getHibernateTemplate().find(strSQL, Zlid);
			if (!tList.isEmpty()) {
				tjyj = tList.get(0);

			}
		} catch (RuntimeException re) {
			log.error("findTjyjByZlid", re);
			throw re;
		}
		return tjyj;
	}

	public Basicinfo findBasicInfoByZlid(String zlid) {

		return (Basicinfo) this.getHibernateTemplate().get(Basicinfo.class,
				zlid);

	}

	public void updateBasicInfoShbz(Basicinfo basicinfo) {
		try {
			this.getHibernateTemplate().saveOrUpdate(basicinfo);
			this.getHibernateTemplate().flush();
			this.getHibernateTemplate().clear();
		} catch (RuntimeException re) {
			log.error("updateBasicInfoShbz", re);
			throw re;
		}

	}

	public void saveTjyj(Tjyj tjyj) {
		try {

			this.getHibernateTemplate().saveOrUpdate(tjyj);
		} catch (RuntimeException re) {
			log.error("saveTjyj", re);
			throw re;
		}
	}

	public void updateTjyj(Tjyj tjyj) {
		try {

			this.getHibernateTemplate().update(tjyj);
		} catch (RuntimeException re) {
			log.error("update", re);
			throw re;
		}

	}

	private static final Log log = LogFactory.getLog(PatentManageDAO.class);

	public void deleteUser(String id, String name) {
		try {

			Users user = (Users) this.getHibernateTemplate().get(Users.class,
					new Integer(id));
			this.getHibernateTemplate().delete(user);

		} catch (RuntimeException re) {
			log.error("deleteUser", re);
			throw re;
		}

	}

	public List<Basicinfo> findBasicinfoPageList(final int firstRow,
			final int maxRow, String hqlwhere) {

		final String queryhql = hqlwhere;

		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryhql);
				query.setFirstResult(firstRow);
				query.setMaxResults(maxRow);
				List<Basicinfo> list = (List<Basicinfo>) query.list();
				return list;
			}
		});
	}

	public int findNoticePageCount(String hqlwhere) {

		try {

			return (Integer) this.getHibernateTemplate().find(hqlwhere).get(0);

		} catch (RuntimeException re) {
			log.error("findbasicinfoPageCount", re);
			throw re;
		}
	}

	public boolean findUsersByName(String name, String password) {
		boolean flag = false;
		String hql = "from Users users where users.name=?";
		List<Users> userList = null;
		Users user = null;
		try {

			userList = this.getHibernateTemplate().find(hql, name);
			if (!userList.isEmpty()) {

				if (userList.get(0).getPassword() != null
						&& userList.get(0).getPassword().equals(password)) {

					flag = true;
				}
			}
		} catch (HibernateException e) {
			log.error("findUsersByName---error");
			throw e;
		}

		return flag;
	}

	public void saveUser(Users users) {
		try {
			this.getHibernateTemplate().save(users);

		} catch (HibernateException e) {
			log.error("saveUser---error");
			throw e;
		}

	}

	public void updateUser(Users user) {
		try {
			this.getHibernateTemplate().update(user);
			this.getHibernateTemplate().flush();
		} catch (HibernateException e) {
			log.error("saveUser---error");
			e.printStackTrace();
			throw e;
		}

	}

	public List<Users> findUser(String qxkw) {
		List<Users> userList = null;
		String hql = "";
		if (qxkw != null && !qxkw.equals("0000")) {

			hql = "from Users users where users.qxkw=" + qxkw;
		} else {

			hql = "from Users users";
		}

		try {
			// this.getSession().getTransaction().begin();
			// Query query = this.getSession().createQuery(hql);
			// if(qxkw!=null&&!qxkw.equals("0000")){
			// query.setString(1, qxkw);
			// }
			// query.list();
			userList = this.getHibernateTemplate().find(hql);
			// this.getSession().getTransaction().commit();
			// this.getSession().flush();
			// this.getHibernateTemplate().clear();
		} catch (HibernateException e) {
			log.error("findUser---error");
			throw e;
		}
		return userList;
	}

	public Users findUsersByValidate(String name, String password) {
		String hql = "from Users users where users.name=?  and users.password=?";
		Users user;
		try {
			user = (Users) this.getHibernateTemplate().find(hql,
					new String[] { name, password }).get(0);

		} catch (HibernateException e) {
			log.error("findUsersByValidate---error");
			throw e;
		}
		return user;
	}

	public boolean findUsersByName(String name) {
		boolean flag = false;
		String hql = "from Users users where users.name=?";
		List<Users> userList = null;

		try {
			userList = (List<Users>) this.getHibernateTemplate()
					.find(hql, name);
			if (!userList.isEmpty()) {
				flag = true;
			}
		} catch (HibernateException e) {
			log.error("findUsersByName---error");
			throw e;
		}

		return flag;
	}

	public Users findUsersById(Integer id) {
		Users user = null;
		try {
			user = (Users) this.getHibernateTemplate().get(Users.class, id);

		} catch (HibernateException e) {
			log.error("findUsersById---error");
			throw e;
		}
		return user;
	}

	public List findBasicInfoByQxkw(String qxkw) {
		String strSQL = "from Basicinfo as basicinfo where basicinfo.qxkw.id=?";

		return this.getHibernateTemplate().find(strSQL, qxkw);
	}

	public List<Qxkw> listQxkwByIdLike(String id) {
		String hql="from  Qxkw q where q.id like ? and q.id not like ?";
		return (List<Qxkw>)this.getHibernateTemplate().find(hql,new String[ ]{id+"%",id});
	}
}